package com.njegovanobradovic.pages.admin;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.njegovanobradovic.entities.Page;
import com.njegovanobradovic.so.GetAll;
import com.njegovanobradovic.so.Save;

public class AddPage {
	@Property
	private Page newPage;
	@Property
	private String pageName;
	@Property
	private String title;
	@Property
	private String description;
	@Property
	private String content;
	@Property
	private int orderNum;
	@Property
	private boolean slideshow;
	@Property
	private boolean aside;
	@Property
	private boolean contactForm;
	@Property
	private boolean active;
	
	@Inject
	private Save<Page> savePage;
	@Inject
	private GetAll<Page> getAllPages;
	
	@Component
    private Form addPageForm;
	
	@SuppressWarnings("unused")
	private void onActivate(){
		try {
			getAllPages.setSearchForType(Page.class);
			orderNum = getAllPages.execute().size()+1;
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	@CommitAfter
	public Object onSubmitFromAddPageForm(){
		try {
			newPage = new Page();
			newPage.setActive(active);
			newPage.setAside(aside);
			newPage.setContent(content);
			newPage.setDescription(description);
			newPage.setOrderNum(orderNum);
			newPage.setPageName(pageName);
			newPage.setSlideshow(slideshow);
			newPage.setTitle(title);
			newPage.setContactForm(contactForm);
			savePage.setObject(newPage);
			savePage.execute();
			return AllPages.class;
		} catch (Exception e) {
			System.out.println("\n\n\n*********************************\nError:\n"+e);
			return null;
		}
	}
}
